﻿Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports ISNet.WebUI.WebGrid
Imports System.Data
Imports ISNet.WebUI.WebTreeView
Imports log4net

Partial Class B06FPrgPD
    Inherits System.Web.UI.Page

    Public AlertText As String
    Public ScriptText As String
    Private Shared logger As ILog = LogManager.GetLogger("B06FPrgPD")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        If IsPostBack = False Then


            'PlanYearsTxt.Text = Session("PlanYear")
            'Session("Versions") = 1
            VersionTxt.Text = Session("Versions")
            ' Session("users") = "bb9222"
            'Session.Add("PlanYear", 2557)
            PlanYearsTxt.Text = Session("PlanYear")

            Label1.Text = "ระบบจัดทำงบประมาณ >> จัดทำงบประมาณ >> สร้างรายการ Funded Program โครงการ"

            Dim adF As New B06FPrgPD_DatasetTableAdapters.FundedProgramProjectsTableAdapter
            Dim dataTable As New B06FPrgPD_Dataset.FundedProgramProjectsDataTable
            dataTable = adF.GetDataIsNull(VersionTxt.Text, PlanYearsTxt.Text)
            ' MsgBox(VersionTxt.Text)
            WebGrid1.DataSource = dataTable
            WebGrid1.RebindDataSource()
            WebGrid1.ClientAction.Refresh()
            If dataTable.Count > 0 Then
                SearhBtn.Visible = True
            End If
        End If
    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource




    End Sub

    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ''Exit Sub
        'If e.Row.Type = RowType.Record Then

        '    e.Row.Cells(0).ForceNoEdit = True

        'End If

    End Sub


    Protected Sub WebGrid1_UpdateRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ' MsgBox(Val(e.Row.Cells(4).Value) & "/" & Val(e.Row.Cells(3).Value))
        Dim id As Decimal
        id = e.Row.KeyValue
        ' MsgBox(e.Row.Cells(3).Value & " " & e.Row.Cells(4).Value & " " & e.Row.Cells(5).Value)
        Dim adapter As New B06FPrgPD_DatasetTableAdapters.FundedProgramProjectsTableAdapter
        adapter.UpdateDiscript(e.Row.Cells(1).Value, e.Row.Cells(2).Value, Date.Now, Session("users"), id)
        logger.Info("User " & Session("users") & " update funded name " & e.Row.Cells(1).Value & " " & e.Row.Cells(2).Value)
    End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then
            Dim ActionType As String = TryCast(Request("type"), String)
            If ActionType = "1" Then
                Dim checkedRows As ArrayList = WebGrid1.RootTable.GetCheckedRows()

                Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter

                For Each keyValue As String In checkedRows

                    adapter.CoppyRow(keyValue)

                Next keyValue
            Else
                Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter
                Dim adapter2 As New fundCenterTableAdapters.AttachmentsTableAdapter
                For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows()
                    adapter2.DeleteAttach(keyValue)
                    adapter.DeleteRow(keyValue)

                Next keyValue
            End If

            WebGrid1.ClientAction.Refresh()
        End If

    End Sub



    Protected Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs) Handles ImageButton1.Click

        If VersionTxt.Text.Trim = "" Then
            Alert("กรุณากรอกเวอร์ชัน")
            Exit Sub
        End If

        Dim adap1 As New B06FPrgPD_DatasetTableAdapters.BudgetDetial1TableAdapter
        Dim adapTmp As New B06FPrgPD_DatasetTableAdapters.Tmp_FundProTableAdapter
        Dim adF As New B06FPrgPD_DatasetTableAdapters.FundedProgramProjectsTableAdapter
        Dim adD As New B06FPrgPD_DatasetTableAdapters.BudgetDetailsTableAdapter
        'Dim ds As New DataSet
        'Dim dt As DataTable = ds.Tables.Add("Items")
        'dt.Columns.Add("FundedProgram", Type.GetType("System.String"))
        'dt.Columns.Add("Description", Type.GetType("System.String"))
        'dt.Columns.Add("ShortDescription", Type.GetType("System.String"))
        'dt.Columns.Add("Id", Type.GetType("System.Int32"))


        adF.DeleteByVersion(VersionTxt.Text, Val(PlanYearsTxt.Text))

        For Each row As B06FPrgPD_Dataset.BudgetDetial1Row In adap1.GetData1(VersionTxt.Text, PlanYearsTxt.Text)
            adF.InsertFundPro(PlanYearsTxt.Text, Val(PlanYearsTxt.Text), VersionTxt.Text, row.ProjectCode.PadRight(12, "0"), If(row.IsDescriptionNull, "", row.Description), If(row.IsShortDescriptionNull, "", row.ShortDescription), Date.Now, Session("users"), row.Id)
            adD.UpdateFundedPro(row.ProjectCode.PadRight(12, "0"), row.Id)
            'Dim r As DataRow
            'r = dt.NewRow()
            'r("FundedProgram") = row.ProjectCode
            'r("Description") = row.Description
            'r("ShortDescription") = ""
            'r("Id") = row.Id
            'dt.Rows.Add(r)
        Next
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Dim codeProj As Integer = 1
        Dim XX As String = ""
        Dim YY As String = Val(PlanYearsTxt.Text)
        YY = Right(YY, 2)
        Dim Pcode As String = ""
        Dim NNN As String = ""
        For Each row As B06FPrgPD_Dataset.BudgetDetial1Row In adap1.GetData2(VersionTxt.Text, PlanYearsTxt.Text)
            XX = "0" & codeProj.ToString
            XX = Right(XX, 2)
            Pcode = "P" & XX & Right(row.TmpFundedProgram, 7)
            Dim check As Integer = 0
            ' Pcode = Left(Pcode, 7)
            check = Val(adapTmp.getCounter(Left(Pcode, 7)))           'ตรวจ 7 หลักแรกใน Tmp
            ' MsgBox("check=" & check)
            If check > 0 Then    'ถ้ามีอยู่แล้วต้อง + runningNumber
                For i = check + 1 To 999
                    NNN = "00" & i
                    NNN = Right(NNN, 3)
                    Pcode = Left(Pcode, 7)

                    Dim count As Integer = Val(adap1.getTmp1(VersionTxt.Text, PlanYearsTxt.Text, Pcode & NNN))
                    If count = 0 Then
                        adapTmp.UpdateCounter(i, Pcode)

                        Pcode = Pcode & NNN
                        Exit For
                    End If
                Next
            Else   'ถ้ายังไม่มี runningNumber=001

                For i = 1 To 999
                    NNN = "00" & i
                    NNN = Right(NNN, 3)
                    Pcode = Left(Pcode, 7)

                    Dim count As Integer = Val(adap1.getTmp1(VersionTxt.Text, PlanYearsTxt.Text, Pcode & NNN))
                    If count = 0 Then
                        adapTmp.InsertTmp_FundPro(Pcode, i)


                        Pcode = Pcode & NNN
                        Exit For
                    End If
                Next
            End If
            Pcode = Pcode.Replace("P", "")
            Pcode = "P" & YY & Pcode
            'Dim r As DataRow
            'r = dt.NewRow()
            'r("FundedProgram") = Pcode
            'r("Description") = row.Description
            'r("ShortDescription") = ""
            'r("Id") = row.Id
            'dt.Rows.Add(r)
            adF.InsertFundPro(PlanYearsTxt.Text, Val(PlanYearsTxt.Text), VersionTxt.Text, Pcode.PadRight(12, "0"), If(row.IsDescriptionNull, "", row.Description), If(row.IsShortDescriptionNull, "", row.ShortDescription), Date.Now, Session("users"), row.Id)
            adD.UpdateFundedPro(Pcode.PadRight(12, "0"), row.Id)
            codeProj += 1
        Next
        NNN = ""
        XX = ""
        Pcode = ""
        codeProj = 1
        For Each row As B06FPrgPD_Dataset.BudgetDetial1Row In adap1.GetData3(VersionTxt.Text, PlanYearsTxt.Text)
            XX = "0" & codeProj.ToString
            XX = Right(XX, 2)
            Pcode = "D" & XX & Right(row.TmpFundedProgram, 7)
            Dim check As Integer = 0

            check = Val(adapTmp.getCounter(Left(Pcode, 7)))           'ตรวจ 7 หลักแรกใน Tmp
            ' MsgBox("check=" & check)
            If check > 0 Then    'ถ้ามีอยู่แล้วต้อง + runningNumber
                For i = check + 1 To 999
                    NNN = "00" & i
                    NNN = Right(NNN, 3)
                    Pcode = Left(Pcode, 7)

                    ' MsgBox("1" & Pcode)
                    Dim count As Integer = Val(adap1.getTmp1(VersionTxt.Text, PlanYearsTxt.Text, Pcode & NNN))
                    If count = 0 Then
                        adapTmp.UpdateCounter(i, Pcode)

                        Pcode = Pcode & NNN
                        Exit For
                    End If

                Next
            Else   'ถ้ายังไม่มี runningNumber=001

                For i = 1 To 999
                    NNN = "00" & i
                    NNN = Right(NNN, 3)
                    Pcode = Left(Pcode, 7)

                    '  MsgBox("2" & Pcode)
                    Dim count As Integer = Val(adap1.getTmp1(VersionTxt.Text, PlanYearsTxt.Text, Pcode & NNN))
                    If count = 0 Then
                        adapTmp.InsertTmp_FundPro(Pcode, i)

                        Pcode = Pcode & NNN
                        Exit For
                    End If

                Next
            End If
            Pcode = Pcode.Replace("D", "")
            Pcode = "D" & YY & Pcode
            'Dim r As DataRow
            'r = dt.NewRow()
            'r("FundedProgram") = Pcode
            'r("Description") = row.Description
            'r("ShortDescription") = ""
            'r("Id") = row.Id
            'dt.Rows.Add(r)
            adF.InsertFundPro(PlanYearsTxt.Text, Val(PlanYearsTxt.Text), VersionTxt.Text, Pcode.PadRight(12, "0"), If(row.IsDescriptionNull, "", row.Description), If(row.IsShortDescriptionNull, "", row.ShortDescription), Date.Now, Session("users"), row.Id)
            adD.UpdateFundedPro(Pcode.PadRight(12, "0"), row.Id)
            codeProj += 1
        Next

        Dim adFRang As New B06FPrgPD_DatasetTableAdapters.Range_FundTableAdapter
        For Each row As B06FPrgPD_Dataset.Range_FundRow In adFRang.GetData(9, VersionTxt.Text, Val(PlanYearsTxt.Text))

            adF.InsertFundPro(PlanYearsTxt.Text, Val(PlanYearsTxt.Text), VersionTxt.Text, row.FundedProgram.PadRight(12, "0"), "", "", Date.Now, Session("users"), Nothing)

        Next
        For Each row As B06FPrgPD_Dataset.Range_FundRow In adFRang.GetData(7, VersionTxt.Text, Val(PlanYearsTxt.Text))

            adF.InsertFundPro(PlanYearsTxt.Text, Val(PlanYearsTxt.Text), VersionTxt.Text, row.FundedProgram.PadRight(12, "0"), "", "", Date.Now, Session("users"), Nothing)

        Next
        For Each row As B06FPrgPD_Dataset.Range_FundRow In adFRang.GetData(5, VersionTxt.Text, Val(PlanYearsTxt.Text))
            ' MsgBox(VersionTxt.Text)
            adF.InsertFundPro(PlanYearsTxt.Text, Val(PlanYearsTxt.Text), VersionTxt.Text, row.FundedProgram.PadRight(12, "0"), "", "", Date.Now, Session("users"), Nothing)

        Next

        logger.Info("User " & Session("users") & " generate funded program")

        adapTmp.DeleteTmP_All()
        Dim dataTable As New B06FPrgPD_Dataset.FundedProgramProjectsDataTable
        dataTable = adF.GetDataIsNull(VersionTxt.Text, PlanYearsTxt.Text)
        ' MsgBox(VersionTxt.Text)
        WebGrid1.DataSource = dataTable
        WebGrid1.RebindDataSource()
        WebGrid1.ClientAction.Refresh()
        Alert("สร้างรหัส FundedProgram โครงการเรียบร้อย \nกรุณาระบุชื่อกลุ่มในรายการที่ไม่มีรายละเอียด")
        If dataTable.Count > 0 Then
            SearhBtn.Visible = True
        End If
    End Sub

    Protected Sub ImageButton2_Click(sender As Object, e As ImageClickEventArgs) Handles ImageButton2.Click
        If VersionTxt.Text.Trim = "" Then
            Alert("กรุณากรอกเวอร์ชัน")
            Exit Sub
        End If
        Dim adF As New B06FPrgPD_DatasetTableAdapters.FundedProgramProjectsTableAdapter
        adF.DeleteByVersion(VersionTxt.Text, Val(PlanYearsTxt.Text))
        logger.Info("User " & Session("users") & " delete funded program")

        Dim dataTable As New B06FPrgPD_Dataset.FundedProgramProjectsDataTable
        WebGrid1.DataSource = dataTable
        WebGrid1.RebindDataSource()
        WebGrid1.ClientAction.Refresh()
        Alert("ลบข้อมูลเรียบร้อย")
    End Sub
    Sub Alert(ByVal message As String)
        AlertText = "<script> alert('" & message & "');</script>"
    End Sub

    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click

    End Sub
End Class
